{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pylab as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = cv2.imread('./images/69.jpg', cv2.IMREAD_GRAYSCALE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "rows, cols = image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "grayHist = np.zeros([256], np.uint64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "for r in range(rows):\n",
    "  for c in range(cols):\n",
    "    grayHist[image[r][c]] += 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render(image):\n",
    "  cv2.imshow('image', image)\n",
    "  cv2.waitKey()\n",
    "  cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "render(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "O(r,c)=a∗I(r,c)+b,0≤r<H,0≤c<W"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "I = image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "O = float(a) * I + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "O[O>255] = 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "O = np.round(O)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "O = O.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cv2.imshow('I', I)\n",
    "cv2.imshow('O', O)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_v1(param_list: list):\n",
    "  for param in param_list:\n",
    "    a = param[0]\n",
    "    b = param[1]\n",
    "    # 线性变换\n",
    "    O = float(a) * I + b\n",
    "    O[O>255] = 255\n",
    "    O = np.round(O)\n",
    "    O = O.astype(np.uint8)\n",
    "    cv2.imshow(f'a={a}, b={b}', O)\n",
    "  cv2.waitKey()\n",
    "  cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_list = []\n",
    "param_list.append((1, 0))\n",
    "param_list.append((1, 20))\n",
    "param_list = set(param_list)\n",
    "cal_v1(param_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_v2(param_list: list, image):\n",
    "  cv2.imshow('origin', image)\n",
    "  for param in param_list:\n",
    "    L = param[0]\n",
    "    H = param[1]\n",
    "    # 白色更白，黑色更黑\n",
    "    O = np.copy(image)\n",
    "    assert id(O) != id(I)\n",
    "    O[O>=H] = 255\n",
    "    O[O<=L] = 0\n",
    "    # O = np.round(O)\n",
    "    O = O.astype(np.uint8)\n",
    "    cv2.imshow(f'L={L}, H={H}', O)\n",
    "  cv2.waitKey()\n",
    "  cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_list = []\n",
    "param_list.append((0, 255))\n",
    "param_list.append((50, 230))\n",
    "param_list = set(param_list)\n",
    "image = cv2.imread('./images/69.jpg', cv2.IMREAD_GRAYSCALE)\n",
    "cal_v2(param_list, image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_v3(param_list: list, image):\n",
    "  \"\"\"伽马变换\"\"\"\n",
    "  cv2.imshow('origin', image)\n",
    "  for param in param_list:\n",
    "    fI = image / 255.0\n",
    "    gamma = param[0] # 0.3\n",
    "    O = np.power(fI, gamma)\n",
    "    # O = O.astype(np.uint8)\n",
    "    print(O)\n",
    "    cv2.imshow(f'gamma: {gamma}', O)\n",
    "  cv2.waitKey()\n",
    "  cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.98050735 0.98050735 0.97084724 ... 0.78105119 0.78105119 0.78105119]\n",
      " [0.95169891 0.94221045 0.93277898 ... 0.70718328 0.70718328 0.70718328]\n",
      " [0.90482535 0.89562068 0.86834469 ... 0.63767378 0.63767378 0.63767378]\n",
      " ...\n",
      " [0.80665753 0.80665753 0.80665753 ... 0.91408655 0.91408655 0.91408655]\n",
      " [0.74767729 0.74767729 0.74767729 ... 0.798067   0.798067   0.798067  ]\n",
      " [0.85044122 0.85044122 0.85044122 ... 0.62281016 0.62281016 0.62281016]]\n"
     ]
    }
   ],
   "source": [
    "param_list = []\n",
    "# param_list.append((0.3, ))\n",
    "# param_list.append((0.6, ))\n",
    "param_list.append((2.5, ))\n",
    "# param_list.append((3, ))\n",
    "param_list = set(param_list)\n",
    "image = cv2.imread('./images/69.jpg', cv2.IMREAD_GRAYSCALE)\n",
    "cal_v3(param_list, image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_list = []\n",
    "param_list.append((1, ))\n",
    "param_list.append((0.3, ))\n",
    "param_list.append((0.6, ))\n",
    "param_list = set(param_list)\n",
    "\n",
    "image = cv2.imread('./images/test.png', cv2.IMREAD_GRAYSCALE)\n",
    "cal_v3(param_list, image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_v4(param_list: list, image, origin_name, extension):\n",
    "  \"\"\"伽马变换\"\"\"\n",
    "  # cv2.imshow('origin', image)\n",
    "  cv2.imwrite(f'output/{origin_name}_origin{extension}', image)\n",
    "  for param in param_list:\n",
    "    fI = image / 255.0\n",
    "    gamma = param[0] # 0.3\n",
    "    O = np.power(fI, gamma)\n",
    "    # cv2.imshow(f'gamma: {gamma}', O)\n",
    "    # need to extent to unit8\n",
    "    O = O * 255\n",
    "    filename = f\"output/{origin_name}_{str(gamma).replace('.', '_')}{extension}\"\n",
    "    status = cv2.imwrite(filename, O)\n",
    "  cv2.waitKey()\n",
    "  cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_list = []\n",
    "param_list.append((2.5, ))\n",
    "# param_list.append((1, ))\n",
    "param_list = set(param_list)\n",
    "filename = '69'\n",
    "extension = '.jpg'\n",
    "image = cv2.imread(f'./images/{filename}{extension}', cv2.IMREAD_GRAYSCALE)\n",
    "cal_v4(param_list, image, filename, extension)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "c8021be2121acb6aedb321c9fc0970845e72ecff144800146fabd90eee2b1d5e"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 64-bit ('learn': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
